package edu.put.sk.nrpp.ospf.packet;

import edu.put.sk.nrpp.*;
import edu.put.sk.nrpp.ospf.packet.lsa.LSA;

/**
 * <pre>
        0                   1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0     0|   Version #   |       2       |         Packet length         |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1    32|                          Router ID                            |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2    64|                           Area ID                             |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3    96|           Checksum            |             AuType            |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
4   128|                       Authentication                          |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
5   160|                       Authentication                          |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
6   192|         Interface MTU         |    Options    |0|0|0|0|0|I|M|MS
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
7   224|                     DD sequence number                        |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8   256|                                                               |
       +-                                                             -+
9   288|                                                               |
       +-                      An LSA Header                          -+
10     |                                                               |
       +-                                                             -+
11     |                                                               |
       +-                                                             -+
12     |                                                               |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                              ...                              |
   </pre>
    The format of the Database Description packet is very similar to
    both the Link State Request and Link State Acknowledgment packets.
    The main part of all three is a list of items, each item describing
    a piece of the link-state database.  The sending of Database
    Description Packets is documented in Section 10.8.  The reception of
    Database Description packets is documented in Section 10.6.
 * @author kevin
 *
 */
final public class OSPFPacketVersion2Type2 extends OSPFPacket{
	
	private LSA lsaData;
	
	public OSPFPacketVersion2Type2(Bit[] data) throws Exception{
		super(data);
		int end=(8*(getPacketLength()))-1;
		Bit[] lsaDataBit=Bit.cut(data, 224, end);
		lsaData = LSA.getInstance(lsaDataBit);
	}
	
	public LSA getLSA(){
		return lsaData;
	}
	
	/**
	 *  I-bit
        The Init bit.  When set to 1, this packet is the first in the
        sequence of Database Description Packets.
	 * @return I-bit
	 */
	
	public int getI(){
		return Bit.toInt(Bit.cut(data, 221, 221));
	}
	/**
	 * M-bit
        The More bit.  When set to 1, it indicates that more Database
        Description Packets are to follow.
	 * @return M-bit
	 */
	public int getM(){
		return Bit.toInt(Bit.cut(data, 222, 222));
	}
	/**
	 *MS-bit
        The Master/Slave bit.  When set to 1, it indicates that the
        router is the master during the Database Exchange process.
        Otherwise, the router is the slave.
	 * @return MS-bit
	 */
	public int getMS(){
		return Bit.toInt(Bit.cut(data, 223, 223));
	}
	
	/**
	 * Options
        The optional capabilities supported by the router, as documented
        in Section A.2.
	 * @return Options
	 */
	public int getOptions(){
		return Bit.toInt(Bit.cut(data, 208, 215));
	}
	
	/**
	 *  DD sequence number
        Used to sequence the collection of Database Description Packets.
        The initial value (indicated by the Init bit being set) should
        be unique.  The DD sequence number then increments until the
        complete database description has been sent.
	 * @return DD sequence number
	 */
	public int getDDSequenceNumber(){
		return Bit.toInt(Bit.cut(data, 224, 255));
	}
	
	/**
	 *  Interface MTU
        The size in bytes of the largest IP datagram that can be sent
        out the associated interface, without fragmentation.  The MTUs
        of common Internet link types can be found in Table 7-1 of
        [Ref22]. Interface MTU should be set to 0 in Database
        Description packets sent over virtual links.
	 * @return Interface MTU
	 */
	public int getInterfaceMTU(){
		return Bit.toInt(Bit.cut(data, 192, 207));
	}
	
}
